package com.vogue.supplymall.account.controller;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.vogue.supplymall.account.domain.model.BaseCode;
import com.vogue.supplymall.account.domain.service.BaseCodeService;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UserInfo;
import com.vogue.supplymall.common.domain.validate.Regex;
import com.vogue.supplymall.common.exception.NeedUploadEntInfoToBeSellException;
import com.vogue.supplymall.match.domain.model.MatchOrgan;
import com.vogue.supplymall.match.domain.service.MatchOrganService;

/**
 * 企业开通卖家身份请求集合
 * @author 崔岳
 *
 */
@Validated
@RestController
public class ToBeSellerController extends BaseController {

    public static final Logger LOGGER_TOBESELLERCONTROLLER = LoggerFactory.getLogger(ToBeSellerController.class);

    @Resource
    private BaseCodeService baseCodeService;

    @Resource
    private MatchOrganService matchOrganService;

    /**
     * 查询是否已经同意开通协议
     * @param appver 版本号
     * @param request 请求对象
     * @return ResponseEntity<ControllerResultInfo>
     * @throws Exception 查询异常
     */
    @RequestMapping("/is/{appver}/account/agreement/isopen")
    public ResponseEntity<ControllerResultInfo> agreementIsopen(
            @PathVariable("appver") @Regex(name = "appver", regexcontent = "vernum")String appver,
            HttpServletRequest request)  throws Exception {
        UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        MatchOrgan organ = matchOrganService.selectOneByWhr("type=1 and syscodeid="
                + user.getSyscodeid());
        if (organ == null) {
            throw new NeedUploadEntInfoToBeSellException();
        }
        BaseCode bc = baseCodeService.get(user.getSyscodeid());
        ControllerResultInfo result = new ControllerResultInfo();
        result.setResult(!bc.getSellerflag());
        return new ResponseEntity<ControllerResultInfo>(result, HttpStatus.OK);
    }

    /**
     * 同意协议开通卖家身份
     * @param appver 版本号
     * @param request 请求对象
     * @return ResponseEntity<ControllerResultInfo>
     * @throws Exception 开通异常
     */
    @RequestMapping("/is/{appver}/account/agreement/open")
    public ResponseEntity<ControllerResultInfo> agreementSave(
            @PathVariable("appver") @Regex(name = "appver", regexcontent = "vernum")String appver,
            HttpServletRequest request)  throws Exception {
        UserInfo user = (UserInfo) SecurityUtils.getSubject().getPrincipal();
        BaseCode bc = baseCodeService.get(user.getSyscodeid());
        bc.setSellerflag(true);
        bc.setBesellerdate(new Date());
        baseCodeService.update(bc);
        ControllerResultInfo result = new ControllerResultInfo();
        result.setResult(true);
        return new ResponseEntity<ControllerResultInfo>(result, HttpStatus.OK);
    }
}
